# coding=utf-8

from multiprocessing import Process
import time
import os
import datetime

# 配置参数, 爬虫名称，运行频率(秒)
custom_conf = [
	{
		"spider_name": "StockSpider",
		"frequency": 1800,  # 60 * 30
	},
]


def start_spider(spider_name, frequency, morning_left, morning_right, noon_left, noon_right):
	args = 'scrapy crawl {0}'.format(spider_name)
	while True:
		now = datetime.datetime.strptime('2019-10-28', '%Y-%m-%d')
		# 星期一至星期五，9：30-11：30/13：00-15：00
		if now.weekday() != 5 and now.weekday() != 6 and morning_left < now < morning_right and noon_left < now < noon_right:
			p = Process(target=os.system, args=(args,))
			p.start()
			p.join()
			print('=' * 100)
			time.sleep(frequency)


if __name__ == '__main__':
	morning_left = datetime.date.today()
	morning_left = morning_left.strftime('%Y-%m-%d') + ' 09:30:00'
	morning_left = datetime.datetime.strptime(morning_left, '%Y-%m-%d %H:%M:%S')
	morning_right = datetime.date.today()
	morning_right = morning_right.strftime('%Y-%m-%d') + ' 11:30:00'
	morning_right = datetime.datetime.strptime(morning_right, '%Y-%m-%d %H:%M:%S')
	noon_left = datetime.date.today()
	noon_left = noon_left.strftime('%Y-%m-%d') + ' 13:00:00'
	noon_left = datetime.datetime.strptime(noon_left, '%Y-%m-%d %H:%M:%S')
	noon_right = datetime.date.today()
	noon_right = noon_right.strftime('%Y-%m-%d') + ' 15:00:00'
	noon_right = datetime.datetime.strptime(noon_right, '%Y-%m-%d %H:%M:%S')
	for conf in custom_conf:
		process = Process(target=start_spider, args=(conf["spider_name"], conf["frequency"], morning_left, morning_right, noon_left, noon_right))
		process.start()
